package top.ivansong92.example.leetcode.learning.string;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestExamp2 {

    @Test
    public void testIsPalindrome() {
        System.out.println(isPalindrome1(12121));
        System.out.println(isPalindrome(12121));
    }

    /**
     * 9. 回文数
     * @param x
     * @return
     */
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        String x_s = "" + x;
        StringBuilder x1_s = new StringBuilder();
        for(int i = x_s.length() - 1; i >=0; i--) {
            x1_s.append(x_s.charAt(i));
        }

        long x1 = Long.parseLong(x1_s.toString());
        return x1 == x;
    }

    public boolean isPalindrome1(int x) {
        if (x < 0) {
            return false;
        }

        int x_d = x / 10;
        int x_t = x % 10;
        long x1 = x_t;
        while (x_d > 0) {
            x_t = x_d % 10;
            x_d = x_d / 10;
            x1 = x1 * 10 + x_t;
        }

        return x1 == x;
    }



    @Test
    public void testRemoveDuplicates() {
        System.out.println(removeDuplicates("azxxzy"));
    }

    public String removeDuplicates(String s) {
        if (s == null || s.length() <= 1) {
            return s;
        }
        char[] chs = s.toCharArray();
        while (true) {

        }
    }

    /**
     * 415. 字符串相加
     */
    public String addStrings(String num1, String num2) {
        char[] ch1 = num1.toCharArray();
        char[] ch2 = num2.toCharArray();
        StringBuilder res = new StringBuilder();
        int i = ch1.length - 1;
        int j = ch2.length - 1;
        int temp = 0;
        while (i >=0 || j >= 0) {
            int val = 0;
            if (i >= 0 && j >= 0) {
                val =  (ch1[i] - '0') + (ch2[j] - '0') + temp;
                i --;
                j --;
            } else if (i >= 0) {
                val =  (ch1[i] - '0') + temp;
                i --;
            } else {
                val =   (ch2[j] - '0') + temp;
                j --;
            }

            res.insert(0, val % 10);
            temp = val > 10 ? 1 : 0;
        }

        if (temp > 0) {
            if (res.length() == Integer.MAX_VALUE) {
                throw new RuntimeException("out of length");
            }
            res.insert(0, '1');
        }
        return res.toString();
    }

    /**
     * 557. 反转字符串中的单词 III
     * @param s
     * @return
     */
    public String reverseWords(String s) {
        if (s == null || s.length() <= 1) {
            return s;
        }
        String[] words = s.split(" ");
        for(int i = 0; i < words.length; i ++) {
            words[i] = reverseWord(words[i]);
        }
        return String.join(" ", words);
    }
    private String reverseWord(String word) {
        StringBuilder res = new StringBuilder();
        for(int i = word.length() - 1; i >= 0; i ++) {
            res.append(word.charAt(i));
        }
        return res.toString();
    }

    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext())
        {
            int input = scanner.nextInt();
            System.out.println(getMaxDrinkNum(input));
        }
        scanner.close();
    }

    private static int getMaxDrinkNum(int n){
        int res = 0, temp = n;
        while(temp >= 3) {
            int t = temp / 3;
            res += t;
            temp = t + temp % 3;
        }
        return res;
    }

    private static int get16To10(String input) {
        if (input == null || input.length() == 0) {
            return -1;
        }
        if (!input.startsWith("0x")) {
            return -1;
        }

        int res = 0;
        int k = 1;
        char[] chars = input.substring(2).toCharArray();
        for(int i = chars.length - 1; i >= 0; i ++) {
            int a1 = chars[i] - '0';
            int a2 = chars[i] - 'A';
            if (a1 <= 9 && a1 >= 0) {//0-9
                res += a1 * k;
            } else if (a2 >= 0 && a2 <= 5) { //A-Z
                res += (a2 + 10) * k;
            } else {
                return -1;
            }
            k *= 16;
        }
        return res;
    }

    /**
     * 833. 字符串中的查找与替换
     * @param s
     * @param indices
     * @param sources
     * @param targets
     * @return
     */
    public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) {
        return "";
    }

    /**
     * 344. 反转字符串
     * https://leetcode.cn/problems/reverse-string/
     * @param s 字符串
     */
    public void reverseString(char[] s) {
        if (s == null || s.length <= 1) {
            return;
        }
        int left = 0,right = s.length - 1;
        while (left < right) {
            swap(s, left, right);
            left ++;
            right --;
        }
    }

    /**
     * 交换字符串数组的第i个元素和 第j个元素
     * @param chars 字符串数组
     * @param i 字符串数组索引i
     * @param j 字符串数组j
     */
    private void swap(char[] chars, int i, int j) {
        char ch = chars[i];
        chars[i] = chars[j];
        chars[j] = ch;
    }
}
